package com.zxkj.commonpatrol.adapt;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.SuperKotlin.pictureviewer.ImagePagerActivity;
import com.allen.library.base.BaseRepertory;
import com.czy.basecommon.utils.tools.MyImageLoader;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.xui.widget.imageview.ImageLoader;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.Group;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;


import net.cachapa.expandablelayout.ExpandableLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


public class MyTextSimpleAdapter extends RecyclerView.Adapter {
    private static final int UNSELECTED = -1;

    private List<HashMap<Integer, Boolean>> pos = new ArrayList<>();
    private boolean isHasData = false;

    private RecyclerView recyclerView;
    private int selectedItem = UNSELECTED;
    private List<Group> data;
    private Context context;
    private LayoutInflater inflater;

    private boolean isNet = false;
    private int index;
    private String code;
    private boolean isInpunt = false;
    private String WTCode;
    private int IdentityType;
    private ArrayList<String> collect;

    public MyTextSimpleAdapter(RecyclerView recyclerView, List<Group> data, Context context) {
        this.recyclerView = recyclerView;
        this.data = data;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (context == null)
            context = parent.getContext();
        if (inflater == null)
            inflater = LayoutInflater.from(context);
        View view;
        switch (viewType) {
            case 0:
                view = inflater.inflate(R.layout.item_spection_d_parent1, parent, false);
                return new SDViewHolder(view);
            case 1:
                view = inflater.inflate(R.layout.item_spection_parent1, parent, false);
                return new NetViewHolder(view);

        }
        return null;

//        View itemView = LayoutInflater.from(parent.getContext())
//                .inflate(R.layout.item_spection_parent, parent, false);
//        return new ViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {


        Log.d("当前的position", position + "");

        if (holder instanceof SDViewHolder) {
            ((SDViewHolder) holder).bind(((SDViewHolder) holder));

            //设置数据

//            if (isInpunt){
//                ((SDViewHolder)holder).mAddress.setVisibility(View.VISIBLE);
//
//            }
            FzAdapt adapt =new FzAdapt(R.layout.text,data.get(position).getmMemberList());

            ((SDViewHolder) holder).recy1.setAdapter(adapt);

            ((SDViewHolder) holder).mTitle.setText(data.get(position).getTitle());

            ((SDViewHolder) holder).mTvId.setText(position + 1 + "");
            ((SDViewHolder) holder).mAddress.setText(data.get(position).getAddress());

            if (data.get(position).isExpand()) {
                ((SDViewHolder) holder).expandableLayout.expand();
            } else {
                ((SDViewHolder) holder).expandableLayout.collapse();
            }



            ((SDViewHolder) holder).rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    removeList(position);
                }
            });


            ((SDViewHolder) holder).rl_look.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {



                }
            });

        } else if (holder instanceof NetViewHolder) {
            ((NetViewHolder) holder).binds(((NetViewHolder) holder));

//            if (data.get(position).getmMemberList() == null) {
//
//
//            } else {
//             //设置数据
//
//            }
            if (data.get(position).isExpand()) {
                ((NetViewHolder) holder).expandableLayout.expand();
            } else {
                ((NetViewHolder) holder).expandableLayout.collapse();
            }

            if (data.get(position).isChecked) {
                ((NetViewHolder) holder).rl1.setVisibility(View.GONE);
                ((NetViewHolder) holder).rl.setVisibility(View.GONE);
                ((NetViewHolder) holder).mTitle.setTextColor(Color.parseColor("#AAAAAA"));
            } else {
                ((NetViewHolder) holder).mTitle.setTextColor(context.getColor(R.color.gray));
                ((NetViewHolder) holder).rl1.setVisibility(View.VISIBLE);
                ((NetViewHolder) holder).rl.setVisibility(View.VISIBLE);

            }
            ((NetViewHolder) holder).mTitle.setText(data.get(position).getTitle());

            ((NetViewHolder) holder).mTvId.setText(position + 1 + "");
            ((NetViewHolder) holder).mAddress.setText(data.get(position).getAddress());

            ((NetViewHolder) holder).rl1.setOnClickListener(v -> {
//

                ((NetViewHolder) holder).mTitle.setTextColor(Color.parseColor("#AAAAAA"));
                ((NetViewHolder) holder).rl1.setVisibility(View.GONE);
                ((NetViewHolder) holder).rl.setVisibility(View.GONE);

                data.get(position).setChecked(true);
                if (  ((NetViewHolder) holder).expandableLayout.isExpanded()) {
                    ((NetViewHolder) holder).expandableLayout.collapse();
                    data.get(position).setExpand(false);
                }
                notifyDataSetChanged();
                LiveEventBus.get("isClick").post(true);
            });

            ((NetViewHolder) holder).rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    removeList(position);
                    LiveEventBus.get("isClick").post(true);
                }
            });


            if (isHasData && data.get(position).isExpand()) {
                ((NetViewHolder) holder).expandableLayout.expand();
            }
        }


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    /**
     * desc:
     * author:mid
     * addtime:2019-11-14 17:29
     * 删除
     */
    public void removeList(int position) {
        data.remove(position);//删除数据源,移除集合中当前下标的数据

        notifyItemRemoved(position);//刷新被删除的地方
        notifyItemRangeChanged(position, getItemCount()); //刷新被删除数据，以及其后面的数据
    }

    //  添加数据
    public void addData(int position, Group grop) {
//        List<Group> data=new ArrayList<>();
//        data.add(grop);
//        recyclerView.notifyItemInserted(data,position);
//      在list中添加数据，并通知条目加入一条
        data.add(data.size(), grop);

//        //添加动画
        notifyItemInserted(data.size());
        notifyItemRangeChanged(data.size(), getItemCount());
    }


    @Override
    public int getItemViewType(int position) {
        if (data.size() > 0) {
            return data.get(position).getTYPE();
        }
        return super.getItemViewType(position);
    }

    public class SDViewHolder extends RecyclerView.ViewHolder {
        private  RecyclerView recy1;

        private ImageView iv_down;
        private ImageView iv_delete;
        private TextView mTvId;
        private TextView mTitle;
        private TextView mAddress;

        private ExpandableLayout expandableLayout;
        private RelativeLayout rl;
        private RelativeLayout rl_look;

        private ConstraintLayout constraintLayout;

        public SDViewHolder(View itemView) {
            super(itemView);
            expandableLayout = itemView.findViewById(R.id.expandableLayout);
            mTvId = itemView.findViewById(R.id.tv_spection_parent_id);
            mTitle = itemView.findViewById(R.id.tv_spection_parent_title);
            mAddress = itemView.findViewById(R.id.tv_spection_parent_er_address);
            rl = itemView.findViewById(R.id.rl_Delete);
            rl_look = itemView.findViewById(R.id.rl_look);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            iv_down = itemView.findViewById(R.id.iv_down);
            constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.cl_parent);
             recy1 = itemView.findViewById(R.id.recy1);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
            recy1.setLayoutManager(linearLayoutManager);
            expandableLayout.setInterpolator(new OvershootInterpolator());

//            if (isInpunt){
//               mAddress.setVisibility(View.VISIBLE);
//
//            }
            constraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

//                    SDViewHolder holder = (SDViewHolder) recyclerView.findViewHolderForAdapterPosition(selectedItem);
//                    if (holder != null) {
//                        holder.constraintLayout.setSelected(false);
//                        holder.expandableLayout.collapse();
//                    }
//
//                    int position = getAdapterPosition();
//                    if (position == selectedItem) {
//                        selectedItem = UNSELECTED;
//                    } else {
//                        constraintLayout.setSelected(true);
//                        expandableLayout.expand();
//                        selectedItem = position;
//                    }

                    if (expandableLayout.isExpanded()) {
                        expandableLayout.collapse();
                        data.get(getLayoutPosition()).setExpand(false);
                        iv_down.setImageResource(R.drawable.down);
                    } else {
                        expandableLayout.expand();
                        data.get(getLayoutPosition()).setExpand(true);
                        iv_down.setImageResource(R.drawable.up);
                    }
                }
            });
        }

        public void bind(SDViewHolder holder) {
            int position = getAdapterPosition();
            boolean isSelected = position == selectedItem;

            holder.constraintLayout.setSelected(isSelected);
            holder.expandableLayout.setExpanded(isSelected, false);
        }


    }

    public class NetViewHolder extends RecyclerView.ViewHolder {

        private  RecyclerView recy1;
        private ImageView iv_net_down;
        private ImageView iv_delete;
        private TextView mTvId;
        private TextView mTitle;
        private TextView mAddress;

        private ExpandableLayout expandableLayout;
        private RelativeLayout rl;
        private RelativeLayout rl1;
        private RecyclerView vioce_DataRecyclerView;
        private RecyclerView pic_DataRecyclerView;

        private ConstraintLayout constraintLayout;

        public NetViewHolder(View itemView) {
            super(itemView);
            iv_net_down = itemView.findViewById(R.id.iv_down);
            expandableLayout = itemView.findViewById(R.id.expandableLayout);
            mTvId = itemView.findViewById(R.id.tv_spection_parent_id);
            mTitle = itemView.findViewById(R.id.tv_spection_parent_title);
            mAddress = itemView.findViewById(R.id.tv_spection_parent_er_address);
            rl = itemView.findViewById(R.id.rl_Delete);
            rl1 = itemView.findViewById(R.id.rl_untreated);

            iv_delete = itemView.findViewById(R.id.iv_delete);
            constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.cl_parent);
            recy1 = itemView.findViewById(R.id.recy1);
            expandableLayout.setInterpolator(new OvershootInterpolator());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
            recy1.setLayoutManager(linearLayoutManager);
            constraintLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

//                    NetViewHolder holder = (NetViewHolder) recyclerView.findViewHolderForAdapterPosition(selectedItem);
//                    if (holder != null) {
//                        holder.constraintLayout.setSelected(false);
//                        holder.expandableLayout.collapse();
//                    }
//
//                    int position = getAdapterPosition();
//                    if (position == selectedItem) {
//                        selectedItem = UNSELECTED;
//                    } else {
//                        constraintLayout.setSelected(true);
//                        expandableLayout.expand();
//                        selectedItem = position;
//                    }
                    if (expandableLayout.isExpanded()) {
                        expandableLayout.collapse();
                        data.get(getLayoutPosition()).setExpand(false);
                        iv_net_down.setImageResource(R.drawable.down);
                    } else {

                        if (data.get(getLayoutPosition()).getmMemberList() == null) {



                        } else {
                            expandableLayout.expand();
                            data.get(getLayoutPosition()).setExpand(true);
                            iv_net_down.setImageResource(R.drawable.up);
                        }


                    }
                }
            });


        }

        public void bind() {
            int position = getAdapterPosition();
            boolean isSelected = position == selectedItem;

            constraintLayout.setSelected(isSelected);
            expandableLayout.setExpanded(isSelected, false);
        }


        public void binds(NetViewHolder holder) {
            int position = getAdapterPosition();
            boolean isSelected = position == selectedItem;

            holder.constraintLayout.setSelected(isSelected);
            holder.expandableLayout.setExpanded(isSelected, false);
        }
    }


    //获取当前的没有修复的网络数据
    public List<Group> getUnCheck() {

        List<Group> groups = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getTYPE() == 1) {
                //网络数据
                groups.add(data.get(i));
            }
        }
        return groups;
    }


    //获取手动添加的数据

    public List<Group> getSDData() {
        List<Group> groups = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getTYPE() == 0) {
                //网络数据
                groups.add(data.get(i));
            }
        }
        return groups;
    }

    //设置当前巡查点的Index
    public void setCheckIndex(int index) {
        this.index = index;
    }

    //设置巡查点的code
    public void setWTJ(String WTCode) {
        this.WTCode = WTCode;
    }

    public void setCheckCode(String code) {
        this.code = code;
    }

    public void setIdentityType(int IdentityType) {
        this.IdentityType = IdentityType;
    }

    //    public int getCheckIndex(){
//        return index;
//    }
//
//    public String getCheckCode(){
//        return  code;
//    }
    public List<Group> getAllData() {
        return data;
    }


    public void setIsInpunt(boolean isInpunt) {
        this.isInpunt = isInpunt;
    }


}
